<template>
  <div class="result-container">
    <div class="title-wrap">
      <h2 class="title">{{$route.query.q}}</h2>
      <span class="sub-title">找到{{count}}个结果</span>
    </div>
    <el-tabs v-model="activeIndex">
      <el-tab-pane label="歌曲" name="songs">
        <table class="el-table">
          <thead>
            <th></th>
            <th>音乐标题</th>
            <th>歌手</th>
            <th>专辑</th>
            <th>时长</th>
          </thead>
          <tbody>
            <tr class="el-table__row" 
              v-for="(item,index) in songList" 
              :key="index"
              @dblclick="playMusic(item.id)"
              >
              <td>{{ index + 1 }}</td>
              <td>
                <div class="song-wrap">
                  <div class="name-wrap">
                    <span>{{item.name}}</span>
                    <span v-if="item.mv!=0" class="iconfont icon-MV"></span>
                  </div>
                  <span v-if="item.alia.length!=0">{{item.alia[0]}}</span>
                </div>
              </td>
              <td>{{item.ar[0].name}}</td>
              <td>{{item.al.name}}</td>
              <td>{{item.dt}}</td>
            </tr>
          </tbody>
        </table>
      </el-tab-pane>
      <el-tab-pane label="歌单" name="lists">
        <div class="items">
          <div class="item" @click="toplaylist(item.id)" v-for="(item,index) in albumnList" :key="index"> 
            <div class="img-wrap">
              <div class="num-wrap">
                播放量:
                <span class="num">{{item.playCount}}</span>
              </div>
              <img :src="item.coverImgUrl" alt="" />
              <span class="iconfont icon-play"></span>
            </div>
            <p class="name">{{item.name}}</p>
          </div>
        </div>
      </el-tab-pane>
      <el-tab-pane label="MV" name="mv">
        <div class="items mv">
          <div class="item" @click="toplaymv(item.id)" v-for="(item,index) in mvList" :key="index">
            <div class="img-wrap">
              <img :src="item.cover" alt="" />
              <span class="iconfont icon-play"></span>
              <div class="num-wrap">
                <div class="iconfont icon-play"></div>
                <div class="num">{{item.playCount}}</div>
              </div>
              <span class="time">{{item.dt}}</span>
            </div>
            <div class="info-wrap">
              <div class="name">{{item.name}}</div>
              <div class="singer">{{item.artists[0].name}}</div>
            </div>
          </div>
        </div>
      </el-tab-pane>
    </el-tabs>
    <!-- 分页器 -->
    <el-pagination
      @current-change="handleCurrentChange"
      background
      layout="prev, pager, next"
      :total="count"
      :current-page="page"
      :page-size="limit"
    >
    </el-pagination>
  </div>
</template>

<script>
import getResult from '../utils/result'
export default {
  name: 'result',
  data() {
    return {
      activeIndex: 'songs',
      songList:[],
      count: 0,
      albumnList:[],
      mvList:[],
      page:1,
      type:1,
      limit:12
    };
  },
  watch:{
    activeIndex(){
      switch(this.activeIndex) {
        case 'songs':
          this.type = 1;
          break;
        case 'lists':
          this.type = 1000;
          break;
        case 'mv':
          this.type = 1004;
          break;
        default:
          break;
      }
      getResult.search(this.$route.query.q,this.limit,this.type,(this.page-1)*this.limit)
      .then(res => {
      if(this.type == 1){
        this.songList = res.data.result.songs;
        this.count = res.data.result.songCount;
        this.songList.forEach(x => {
          x.dt = this.formatTime(x.dt);
        })
      } else if(this.type == 1000){
        this.albumnList = res.data.result.playlists;
        this.count = res.data.result.playlistCount;
        this.albumnList.forEach(x => {
          if(x.playCount >= 100000){
            x.playCount = parseInt(x.playCount/10000) + '万';
          }
          x.dt = this.formatTime(x.dt);
        })
      } else if(this.type == 1004) {
        this.mvList = res.data.result.mvs;
        this.count = res.data.result.mvCount;
        this.mvList.forEach(x => {
          if(x.playCount >= 100000){
            x.playCount = parseInt(x.playCount/10000) + '万';
          }
          x.duration = this.formatTime(x.duration);
        })
      }  
    })
    this.page = 1;
    }
  },
  created() {
    getResult.search(this.$route.query.q,this.limit,this.page,(this.page-1)*this.limit)
    .then(res => {
      this.songList = res.data.result.songs;
      this.count = res.data.result.songCount;
      this.songList.forEach(x => {
        x.dt = this.formatTime(x.dt);
      })
    })
  },
  methods: {
    formatTime(time) {
      let min = parseInt(time/1000/60);
      let sec = parseInt(time/1000%60);
      if(min < 10) {
        min = '0' + min;
      } 
      if(sec < 10) {
        sec = '0' + sec;
      }
      return (min + ':' + sec);
    },
    playMusic(id){
      getResult.play(id).then(res => {
        console.log(res.data.data[0].url);
        this.$parent.musicUrl = res.data.data[0].url;
      })
    },
    toplaylist(id) {
      this.$router.push(`/playlist?q=${id}`);
    },
    toplaymv(mvid) {
      this.$router.push(`/mv?q=${mvid}`);
    },
    handleCurrentChange(val){
      this.page = val;
      getResult.search(this.$route.query.q,this.limit,this.type,(this.page-1)*this.limit)
      .then(res => {
      if(this.type == 1){
        this.songList = res.data.result.songs;
        this.count = res.data.result.songCount;
        this.songList.forEach(x => {
          x.dt = this.formatTime(x.dt);
        })
      } else if(this.type == 1000){
        this.albumnList = res.data.result.playlists;
        this.count = res.data.result.playlistCount;
        this.albumnList.forEach(x => {
          if(x.playCount >= 100000){
            x.playCount = parseInt(x.playCount/10000) + '万';
          }
          x.dt = this.formatTime(x.dt);
        })
      } else if(this.type == 1004) {
        this.mvList = res.data.result.mvs;
        this.count = res.data.result.mvCount;
        this.mvList.forEach(x => {
          if(x.playCount >= 100000){
            x.playCount = parseInt(x.playCount/10000) + '万';
          }
          x.duration = this.formatTime(x.duration);
        })
      }  
    })
    }
  },
};
</script>

<style scoped>
.el-table__row td {
  padding: 10px auto;
}
</style>
